﻿//Ana Isabel Ibáñez García
//version 1: create the control and add to the main window. Add all the necesary controls to modify
//the data class
//version 6: added attributes: editionClass,classData,temporalAttributes
//added methods: AddAttribute, btnModifyAttributes_Click, ModifyAttribute,Refresh,btnDeleteAtrribute_Click,btnUpdate_Click_1
//version 7: changed method refresh() to refresh methods as well, btnModifyMethod_Click
//added methods: btnDeleteAttribute_Click, AddMethods, EditMethod

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UMLDesigner
{
    partial class FormControl : UserControl
    {
        private ClassView editionClass;
        private ClassData classData;
        private List<AttributeData> temporalAttributes = new List<AttributeData>();
        private List<MethodData> temporalMethods = new List<MethodData>();
        private List<RelationView> editionRelations = new List<RelationView>();
        private List<RelationView> referencedRelations = new List<RelationView>();
        
        public FormControl()
        {
            InitializeComponent();
            comboVisibility.DataSource = VisibilityType.GetTypes();
            comboModifiers.DataSource = ModifiersTypes.GetTypes();
        }


        //refresh the form with data of selected class
        public void Refresh(ClassView classView)
        {
            editionClass = classView;
            classData = classView.GetClassData();

            txtClassName.Text = classData.GetName();

            comboVisibility.SelectedItem = classData.GetVisibility();
            comboModifiers.SelectedItem = classData.GetModifier();

            listBoxAttributes.Items.Clear();
            foreach (AttributeData attr in classData.GetAttributes())
            {
                listBoxAttributes.Items.Add(attr.GetUMLRepresentation());
            }

            temporalAttributes.Clear();
            for (int i = 0; i < classData.GetAttributes().Count; i++)
            {
                temporalAttributes.Add(classData.GetAttributes()[i]);
            }
            listBoxMethods.Items.Clear();
            foreach (MethodData mtd in classData.GetMethods())
            {
                listBoxMethods.Items.Add(mtd.GetUMLRepresentation());
            }

            temporalMethods.Clear();
            for (int i = 0; i < classData.GetMethods().Count; i++)
            {
                temporalMethods.Add(classData.GetMethods()[i]);
            }

            listBoxRelations.Items.Clear();
            editionRelations.Clear();
            referencedRelations.Clear();

            foreach (RelationView rel in WindowsManager.GetRelations())
            {
                if (rel.GetEndClass() == editionClass)
                {
                    editionRelations.Add(rel);
                    referencedRelations.Add(rel);
                    listBoxRelations.Items.Add(rel.GetIniClass().GetClassData().GetName() + " (" + rel.GetRelationType() + ")");
                }
                else if (rel.GetIniClass() == editionClass)
                {
                    listBoxRelations.Items.Add(rel.GetEndClass().GetClassData().GetName() + " (" + rel.GetRelationType() + ")");
                    editionRelations.Add(rel);
                    referencedRelations.Add(rel);
                }
            }

        }

        private void buttonAddAtributes_Click(object sender, EventArgs e)
        {
            AttributesWindow attribute = new AttributesWindow();
            attribute.Show();
        }


        public void AddAttribute(string name, string type, string visibility)
        {
            AttributeData attribute = new AttributeData(name, type, visibility);
            listBoxAttributes.Items.Add(attribute.GetUMLRepresentation());
            temporalAttributes.Add(attribute);
        }

       

        public void ModifyAttribute(string name, string type, string visibility)
        {
            int index = listBoxAttributes.SelectedIndex;
            temporalAttributes[index].SetName(name);
            temporalAttributes[index].SetAttrType(type);
            temporalAttributes[index].SetVisibility(visibility);
            listBoxAttributes.Items[index] = temporalAttributes[index].GetUMLRepresentation();
        }

     

        private void btnAddMethod_Click(object sender, EventArgs e)
        {
            MethodsWindow methods = new MethodsWindow();
            methods.Show();
        }

       
        private void btnUpdate_Click_1(object sender, EventArgs e)
        {
            //add the data in the form to the structure of the data of the class
            if (editionClass != null)
            {
                classData.SetName(txtClassName.Text);
                classData.SetAttributes(temporalAttributes);
                classData.SetModifier(comboModifiers.Text);
                classData.SetVisibility(comboVisibility.Text);
                classData.SetMethods(temporalMethods);
                editionClass.Refresh();

                //Edit the relations data
                //Delete the original relations
                foreach (RelationView rel in referencedRelations)
                {
                    WindowsManager.GetRelations().Remove(rel);
                }
                //Clear the referenced relations
                referencedRelations.Clear();

                //Add the modified relations
                foreach (RelationView rel in editionRelations)
                {
                    //Update the referenced relations with new values
                    referencedRelations.Add(rel);
                    WindowsManager.GetRelations().Add(rel);
                }
                WindowsManager.PaintRelations();
            }
        }

        private void btnDeleteAttribute_Click(object sender, EventArgs e)
        {
            if (listBoxAttributes.SelectedItem != null)
            {
                //search the attribute to delete
                for (int i = 0; i < temporalAttributes.Count; i++)
                {
                    if (temporalAttributes[i].GetUMLRepresentation() == listBoxAttributes.Text)
                    {
                        temporalAttributes.Remove(temporalAttributes[i]);
                    }
                }

                listBoxAttributes.Items.Remove(listBoxAttributes.SelectedItem);
            }
        }

        private void btnModifyAttribute_Click(object sender, EventArgs e)
        {
            if (listBoxAttributes.SelectedItem != null)
            {
                int index = listBoxAttributes.SelectedIndex;
                AttributesWindow addAttributes = new AttributesWindow(temporalAttributes[index]);
                addAttributes.Show();
            }
        }

        public void AddMethods(string visibility, string name, List<ParameterData> parameters, string returnedValue)
        {
            MethodData metodo = new MethodData(visibility, name, returnedValue);
            metodo.SetParameters(parameters);
            listBoxMethods.Items.Add(metodo.GetUMLRepresentation());
            temporalMethods.Add(metodo);
        }
        public void EditMethod(string visibility, string name, List<ParameterData> parameters, string returnedValue)
        {
            int indice = listBoxMethods.SelectedIndex;
            temporalMethods[indice].SetName(name);
            temporalMethods[indice].SetReturnType(returnedValue);
            temporalMethods[indice].SetParameters(parameters);
            temporalMethods[indice].SetVisibility(visibility);
            listBoxMethods.Items[indice] = temporalMethods[indice].GetUMLRepresentation();
        }

      

        private void btnDeleteMethod_Click_1(object sender, EventArgs e)
        {
            if (listBoxMethods.SelectedItem != null)
            {
                for (int i = 0; i < temporalMethods.Count; i++)
                {
                    if (temporalMethods[i].GetUMLRepresentation() == listBoxMethods.Text)
                    {
                        temporalMethods.Remove(temporalMethods[i]);
                    }
                }

                listBoxMethods.Items.Remove(listBoxMethods.SelectedItem);
            }
        }

        private void btnModifyMethod_Click(object sender, EventArgs e)
        {
            if (listBoxMethods.SelectedItem != null)
            {
                int index = listBoxMethods.SelectedIndex;
                MethodsWindow editMethod = new MethodsWindow(temporalMethods[index]);
                editMethod.Show();
            }
        }

       
        public void EditRelation(string name, string iniCard, string endCard, string iniAttr, string endAttr, string iniAttrVisi, string endAttrVisi)
        {
            int index = listBoxRelations.SelectedIndex;

            RelationView origRelView = editionRelations[index];
            RelationView relView = origRelView.CreateObject();
            relView.SetIniClass(origRelView.GetIniClass());
            relView.SetEndClass(origRelView.GetEndClass());

            RelationshipData data = new RelationshipData();
            data.SetName(name);
            data.SetCardinalityIni(iniCard);
            data.SetCardinalityEnd(endCard);
            data.SetAttrNameIni(iniAttr);
            data.SetAttrNameEnd(endAttr);
            data.SetVisibilityIniAttribute(iniAttrVisi);
            data.SetVisibilityEndAttribute(endAttrVisi);
            relView.SetRelationData(data);

            editionRelations[index] = relView;
        }


        private void buttonDeleteRelation_Click(object sender, EventArgs e)
        {
            if (listBoxRelations.SelectedItem != null)
            {
                int index = listBoxRelations.SelectedIndex;
                editionRelations.RemoveAt(index);

                listBoxRelations.Items.Remove(listBoxRelations.SelectedItem);
            }
        }

        private void buttonEditRelation_Click(object sender, EventArgs e)
        {
            if (listBoxRelations.SelectedItem != null)
            {
                int index = listBoxRelations.SelectedIndex;
                if (!(editionRelations[index] is InheritanceView))
                {
                    RelationWindow editRelation = new RelationWindow(editionRelations[index]);
                    editRelation.Show();
                }
            }
        }

        private void btnDeleteClass_Click(object sender, EventArgs e)
        {
            WindowsManager.DeleteClass();
        }
    }
}
